﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChoWorkflowServer.Model;
using Cinchoo.Core.Xml.Serialization;
using System.Data.SqlServerCe;
using Cinchoo.Core;
using Cinchoo.Core.Configuration;
using Cinchoo.Core.Diagnostics;

namespace ChoWorkflowServer.ModelSqlCE
{
    [ChoNameValueConfigurationSection("sqlCEDataModelSettings", BindingMode = ChoConfigurationBindingMode.OneWay, Silent = false)]
    public class ChoSqlCEDataModelSettings : ChoConfigurableObject
    {
        [ChoPropertyInfo("connectionString")]
        [ChoNotNullOrEmptyValidator]
        public string ConnectionString;
    }

    public class ChoDataModel : ChoDataModelImpl
    {
        private static readonly ChoSqlCEDataModelSettings _sqlCEDataModelSettings = new ChoSqlCEDataModelSettings();
        
        public ChoDataModel()
        {
        }

        public override bool IsTaskExists(string taskName, out bool isDeleted)
        {
            ChoGuard.ArgumentNotNullOrEmpty(taskName, "TaskName");

            try
            {
                isDeleted = false;
                using (SqlCeConnection conn = Connection as SqlCeConnection)
                {
                    conn.Open();

                    using (SqlCeCommand cmd = new SqlCeCommand(ChoDataModelSql.Cho_IS_TASK_EXISTS, conn))
                    {
                        cmd.CommandType = System.Data.CommandType.Text;
                        cmd.Parameters.AddWithValue("@TASK_NAME", taskName);

                        object deleted = cmd.ExecuteScalar();
                        if (deleted != null)
                            isDeleted = Convert.ToBoolean(deleted);
                        return deleted != null;
                    }
                }
            }
            catch (Exception ex)
            {
                ChoTrace.Write(ex);
                throw;
            }
        }

        protected override int UpdateTask(string taskName, string workflowName, string taskDesc, int taskGroupId, string cmdParameters, int? maxNoOfRetry, int? sleepBetweenRetry, int? timeout, string machineName, string modifiedBy, System.Data.IDbConnection conn, System.Data.IDbTransaction trans)
        {
            ChoGuard.ArgumentNotNullOrEmpty(taskName, "TaskName");
            ChoGuard.ArgumentNotNullOrEmpty(modifiedBy, "ModifiedBy");

            try
            {
                using (SqlCeCommand cmd = new SqlCeCommand(ChoDataModelSql.Cho_ADD_OR_UPDATE_TASK, conn as SqlCeConnection))
                {
                    cmd.Transaction = trans as SqlCeTransaction;

                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    cmd.Parameters.AddWithValue("@TASK_NAME", taskName.ToDbValue<string>());
                    cmd.Parameters.AddWithValue("@WORKFLOW_NAME", workflowName.ToDbValue<string>());
                    cmd.Parameters.AddWithValue("@TASK_DESC", taskDesc.ToDbValue<string>());
                    cmd.Parameters.AddWithValue("@TASK_GRP_ID", taskGroupId.ToDbValue<int>());
                    cmd.Parameters.AddWithValue("@CMD_PARAMETERS", cmdParameters.ToDbValue<string>());
                    cmd.Parameters.AddWithValue("@NO_OF_RETRY", maxNoOfRetry.ToDbValue());
                    cmd.Parameters.AddWithValue("@SLEEP_BETWEEN_RETRY", sleepBetweenRetry.ToDbValue());
                    cmd.Parameters.AddWithValue("@TIMEOUT", timeout.ToDbValue());
                    cmd.Parameters.AddWithValue("@MACHINE_NAME", machineName.ToDbValue<string>());
                    cmd.Parameters.AddWithValue("@USER_NAME", modifiedBy.ToDbValue<string>());
                    SqlCeParameter param = new SqlCeParameter("@TASK_ID", 0);
                    //param.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(param);

                    cmd.ExecuteNonQuery();

                    int taskId = Convert.ToInt32(param.Value);

                    return taskId;
                }
            }
            catch (Exception ex)
            {
                ChoTrace.Write(ex);
                throw;
            }
        }

        protected override int AddOrReplaceTaskSchedule(ChoTasksDataSet.Cho_TASK_SCHEDULESRow row, System.Data.IDbConnection conn, System.Data.IDbTransaction trans, string modifiedBy)
        {
            throw new NotImplementedException();
        }

        protected override int DeleteTaskSchedule(ChoTasksDataSet.Cho_TASK_SCHEDULESRow row, System.Data.IDbConnection conn, System.Data.IDbTransaction trans, string modifiedBy)
        {
            throw new NotImplementedException();
        }

        public override int CloneTask(string taskName, string newTaskName, string taskGroupName, string modifiedBy)
        {
            throw new NotImplementedException();
        }

        public override void DeleteTask(int taskId, string modifiedBy)
        {
            throw new NotImplementedException();
        }

        public override ChoTasksDataSet.Cho_TASKSDataTable GetAllTasks(string taskName, string taskGrpName, string machineName, DateTime modifiedDate)
        {
            throw new NotImplementedException();
        }

        public override void UndeleteTask(string taskName, string modifiedBy)
        {
            throw new NotImplementedException();
        }

        public override void SetTaskStatus(int batchId, string taskName, string scheduleName, Cinchoo.Core.ETL.ChoETLWorkflowStatus taskStatus, string modifiedBy, string logMsg = null, DateTime? nextRunTime = null)
        {
            throw new NotImplementedException();
        }

        public override void AddOrUpdateTaskBatch(int batchId, string taskName, string scheduleName, int taskStatus, string cmdParameters, string logMsg, DateTime processStart, DateTime? processEnd, TimeSpan? duration, DateTime? nextRunTime, int exitCode, int? retryCount, string modifiedBy)
        {
            throw new NotImplementedException();
        }

        public override int AddOrReplaceTaskGroup(string taskGroupName, string taskGroupDesc, string modifiedBy)
        {
            throw new NotImplementedException();
        }

        public override int DeleteTaskGroup(string taskGroupName, string modifiedBy)
        {
            throw new NotImplementedException();
        }

        public override bool IsTaskGroupExists(string taskGroupName, out bool isDeleted)
        {
            throw new NotImplementedException();
        }

        public override void UndeleteTaskGroup(string taskGroupName, string modifiedBy)
        {
            throw new NotImplementedException();
        }

        public override void MoveTask(string taskName, int srcTaskGrpId, int destTaskGrpId, string modifiedBy)
        {
            throw new NotImplementedException();
        }

        public override ChoTasksDataSet.Cho_HOLIDAY_CALENDARSDataTable GetAllHolidayCalendars()
        {
            throw new NotImplementedException();
        }

        protected override int AddOrReplaceHolidayCalendar(ChoTasksDataSet.Cho_HOLIDAY_CALENDARSRow row, string modifiedBy, System.Data.IDbConnection conn, System.Data.IDbTransaction trans)
        {
            throw new NotImplementedException();
        }

        protected override int DeleteHolidayCalendar(ChoTasksDataSet.Cho_HOLIDAY_CALENDARSRow row, string modifiedBy, System.Data.IDbConnection conn, System.Data.IDbTransaction trans)
        {
            throw new NotImplementedException();
        }

        protected override void AddOrReplaceHolidayCalendarDate(ChoTasksDataSet.Cho_HOLIDAY_CALENDAR_DATESRow row, string modifiedBy, System.Data.IDbConnection conn, System.Data.IDbTransaction trans)
        {
            throw new NotImplementedException();
        }

        protected override void DeleteHolidayCalendarDate(ChoTasksDataSet.Cho_HOLIDAY_CALENDAR_DATESRow row, string modifiedBy, System.Data.IDbConnection conn, System.Data.IDbTransaction trans)
        {
            throw new NotImplementedException();
        }

        public override ChoTasksDataSet.Cho_HOLIDAY_CALENDAR_DATESDataTable GetAllHolidayCalendarDatesById(int holidayCalId)
        {
            throw new NotImplementedException();
        }

        public override ChoTasksDataSet.Cho_HOLIDAY_CALENDAR_DATESDataTable GetAllHolidayCalendarDatesByName(string holidayCalName)
        {
            throw new NotImplementedException();
        }

        public override int CloneCalendar(string calName, string newCalName, string modifiedBy)
        {
            throw new NotImplementedException();
        }

        public override ChoTasksDataSet.Cho_TASK_SCHEDULESDataTable GetTaskSchedules(int taskId, string scheduleName)
        {
            throw new NotImplementedException();
        }

        public override ChoTasksDataSet.Cho_TASK_SCHEDULE_TYPESDataTable GetTaskScheduleTypes()
        {
            throw new NotImplementedException();
        }

        public override ChoTasksDataSet.Cho_POLICYDataTable GetAllPolicy()
        {
            throw new NotImplementedException();
        }

        public override ChoTasksDataSet.Cho_USERSDataTable GetAllUsers()
        {
            throw new NotImplementedException();
        }

        protected override int AddOrReplaceUser(ChoTasksDataSet.Cho_USERSRow row, System.Data.IDbConnection conn, System.Data.IDbTransaction trans)
        {
            throw new NotImplementedException();
        }

        protected override void DeleteUser(ChoTasksDataSet.Cho_USERSRow row, System.Data.IDbConnection conn, System.Data.IDbTransaction trans)
        {
            throw new NotImplementedException();
        }

        public override ChoTasksDataSet.Cho_ROLESDataTable GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override ChoTasksDataSet.Cho_USER_ROLEDataTable GetAllUserRoles(int roleId)
        {
            throw new NotImplementedException();
        }

        public override ChoTasksDataSet.Cho_ROLE_POLICYDataTable GetAllRolePolicy(int roleId)
        {
            throw new NotImplementedException();
        }

        protected override int AddOrUpdateRole(ChoTasksDataSet.Cho_ROLESRow row, System.Data.IDbConnection conn, System.Data.IDbTransaction trans)
        {
            throw new NotImplementedException();
        }

        protected override void DeleteRole(ChoTasksDataSet.Cho_ROLESRow row, System.Data.IDbConnection conn, System.Data.IDbTransaction trans)
        {
            throw new NotImplementedException();
        }

        protected override void AddRolePolicy(ChoTasksDataSet.Cho_ROLE_POLICYRow row, System.Data.IDbConnection conn, System.Data.IDbTransaction trans)
        {
            throw new NotImplementedException();
        }

        protected override void DeleteRolePolicy(ChoTasksDataSet.Cho_ROLE_POLICYRow row, System.Data.IDbConnection conn, System.Data.IDbTransaction trans)
        {
            throw new NotImplementedException();
        }

        protected override void AddUserRole(ChoTasksDataSet.Cho_USER_ROLERow row, System.Data.IDbConnection conn, System.Data.IDbTransaction trans)
        {
            throw new NotImplementedException();
        }

        protected override void DeleteUserRole(ChoTasksDataSet.Cho_USER_ROLERow row, System.Data.IDbConnection conn, System.Data.IDbTransaction trans)
        {
            throw new NotImplementedException();
        }

        public override ChoTasksDataSet.Cho_POLICYDataTable GetPolicyForUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override ChoTasksDataSet.Cho_DEPENDENCY_TASKSDataTable GetAllDepedencyTasks(string jobName)
        {
            throw new NotImplementedException();
        }

        public override System.Data.IDbConnection Connection
        {
            get { return new SqlCeConnection(_sqlCEDataModelSettings.ConnectionString); }
        }
    }
}
